<?php
!defined('DEBUG') AND exit('Access Denied.');

// 低版本兼容 PHP 5.4
if(!function_exists('array_column')){
	function array_column($arr,$key){ return array_map(function($val) use ($key){ return $val[$key]; },$arr); }
}

$action = param(1);
$kv = kv_cache_get('git_tags');
// hook tag_start.php
if ($action=='add'){
	/*
	 * 增加标签 ( 操作封装到函数, 底部 )
	 * 返回标签ID
	 * 重复检查交给客户端完成
	 * 重复提交仍被记录
	 */

	$tid = param('tid', 0);
	$tag = param('tag');
	empty($uid) AND message(-1, "未登录");	
	tagb($tag,$kv['limit']) AND message(-1, "非法字符或长度");
	tagc($tag,$kv['stop']) AND message(-1, "禁止使用的词语");
	taga($uid,$tid,$gid,$kv['lock']) AND message(-1, "TAG锁");

	$n = db_count('git_tags_thread', array('tid'=>$tid));
	if ( $n > 11 ) message(-1, "标签最大12个");
	// 标签重复(待修正)
	$k = db_find_one('git_tags', array('name'=>$tag));
	if( $k == FALSE ){
		$tagid = db_insert('git_tags',array('name'=>$tag, 'link'=>1));
		db_insert('git_tags_thread',array('tid'=>$tid, 'tagid'=>$tagid));
		// 记录到历史 (创建标签时记录初始时间戳)
	}else{
		$tagid = $k['tagid'];
		db_insert('git_tags_thread',array('tid'=>$tid, 'tagid'=>$tagid));
		db_update('git_tags', array('tagid'=>$tagid), array('link+'=>1));
		// 记录到历史 (新增标签时记录初始时间戳)
	}

	message(1, $tagid);

}elseif ($action=='del') {
	// 删除
	$tid  = param('tid', 0);
	$tag = param('tag', 0);
	empty($uid) AND message(-1, "未登录");
	// 权限 (一般只有作者有权删除)

	// 删主题标签并检查使用使用此标签的其他主题, 否则删
	db_delete('git_tags_thread', array('tid'=>$tid,'tagid'=>$tagid));
	$n = db_find_one('git_tags_thread', array('tid'=>$tid));
	if ( $n == FALSE ){
		db_delete('git_tags_thread', array('tagid'=>$tagid));
	}

	message(1, 'ok');
}elseif (is_numeric($action)){
	// 查询
	$tag = db_find_one('git_tags', array('tagid'=>$action));
	$header['title'] = $tag['name'];
	$header['mobile_title'] = $tag['name'];
	$header['mobile_link']= "#";
	empty($tag) AND message(-1, 'TAG 不存在');
	$arr = db_find('git_tags_thread', array('tagid'=>$action));
	empty($arr) AND message(-1, 'TAG 无目标主题');
	$tid = array_column($arr, 'tid');
	$list = db_find('thread',array('tid'=>$tid));
	empty($list) AND message(-1, 'TGA 有主题但主题已被删');

	// 输出
	if($ajax && !isset($_SERVER['HTTP_X_PJAX'])) message(0, $list);
	include _include(APP_PATH.'plugin/git_tags/view/htm/tag.htm');
}else{
	// 未输入参数或错误的参数
	message(-1, "なてて？");
}

// hook tag_end.php

function taga($uid,$tid,$gid,$lock) {
	/* 若是管理员或作者
	 * 若是 TAG 全局未锁且作者未锁
	 * 否则锁了 
	 */
	$t = '1'; // 锁状态应从设置库读取
	$k = db_find_one('thread', array('tid'=>$tid, 'uid'=>$uid));
	if ($k != FALSE  || $gid == 1) { return FALSE; }
	if ($lock==1 && $t==1){ return FALSE; }
	return true;
}

function tagb($tag,$limit){
	// 小于2和大于24字符数禁止, 过滤字应当从设置的库读取
	if( strlen($tag)<2 || strlen($tag)>$limit)return true;
	// 危险字符过滤 (可能并不严谨)
	$text=$tag;
	if(trim($text)=='')return true;
	$text=preg_replace("/[[:punct:]\s]/",' ',$text);
	$text=urlencode($text);
	$text=preg_replace("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|\-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|\.|%2F|%A3%BF|%A1%B7|%A1%B6|%A1%A2|%A1%A3|%A3%AC|%7D|%A1%B0|%A3%BA|%A3%BB|%A1%AE|%A1%AF|%A1%B1|%A3%FC|%A3%BD|%A1%AA|%A3%A9|%A3%A8|%A1%AD|%A3%A4|%A1%A4|%A3%A1|%E3%80%82|%EF%BC%81|%EF%BC%8C|%EF%BC%9B|%EF%BC%9F|%EF%BC%9A|%E3%80%81|%E2%80%A6%E2%80%A6|%E2%80%9D|%E2%80%9C|%E2%80%98|%E2%80%99|%EF%BD%9E|%EF%BC%8E|%EF%BC%88)+/",' ',$text);
	$text=urldecode($text);
	if ($tag !== trim($text)) { return true; }
	return FALSE;
}

function tagc($tag,$stop){
	// 此处检测 敏♂感 词汇, $stop是存储的字符串
	$stop = array_unique(explode(' ',$stop));
	foreach ($stop as $val) {
		$emmm = strpos($tag,$val);
		if($emmm !== FALSE){ return true; }
	}
	return FALSE;	
}